﻿using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;
public class ZJHCards : MonoBehaviour {

	private int cardValue = -1;
	public int Value = -1;
	private bool Selected = false;

	void Awake () {
		
	}

	public void setSelect(){
		if ( Selected ) {
			Selected = false;
			transform.localPosition = new Vector3 (transform.localPosition.x, transform.localPosition.y - 30, 0);
		} else {
			Selected = true;
			transform.localPosition = new Vector3 (transform.localPosition.x, transform.localPosition.y + 30, 0);
		}
	}

	public bool getSelect(){
		return Selected;
	}

	public void setValue (int value){

		cardValue = value;

		if (cardValue > 0 && cardValue < 14) {
			Value = cardValue;
		} else if (cardValue >= 14 && cardValue < 27) {
			Value = cardValue - 13;
		} else if(cardValue >= 27 && cardValue < 40){
			Value = cardValue - 26;
		}else if(cardValue >= 40 && cardValue < 53){
			Value = cardValue - 39;
		}
	}

	public int getValue (){
		return cardValue;
	}

	public void ChangeStyle(string str){
		Sprite texture = (Sprite)Resources.Load("image/Poker/"+str,typeof(Sprite));
		gameObject.GetComponent<Image>().sprite = texture;
	}

	public void JumpAction(){
		transform.DOLocalMove (new Vector3(transform.localPosition.x,transform.localPosition.y+50,transform.localPosition.z), 0.35f);
		InvokeRepeating ("JumpBack",0.4f,0f);
	}

	public void JumpBack(){
		transform.DOLocalMove (new Vector3(transform.localPosition.x,transform.localPosition.y-50,transform.localPosition.z), 0.35f);
	}

	public void DoClose(){
		ChangeStyle ("CardBlack");
	}

	public void startRotate(int str){
		Tweener tween =	transform.DOLocalRotate (new Vector3 (0, 90, 0),0.4f);
		tween.onComplete = delegate() {
			ChangeStyle(CheckCard(str));
			gameObject.transform.DOLocalRotate (new Vector3 (0, 0, 0),0.4f);
		};
	}

	public void startRotate2(string str,int x){
		transform.DOLocalMove (new Vector3(x,0,0), 0.2f);
		ChangeStyle(str);
	}

	public void startRotate3(int x){
		transform.DOLocalMove (new Vector3(x,0,0), 0.2f);
	}

	public static string CheckCard(int value){
		return value.ToString ();
		/*
		1 - 13 是红桃2-A 
		14 - 26 是黑桃 2-A
		27- 39 是方片2-A
		40 - 52 是 梅花 2-A
		*/
	}
	
	// Update is called once per frame
	void Update () {
		Color color = GetComponent<Image> ().color;
		if (cardValue == -1) {
			color.a = 0;
		} else {
			color.a = 1;
		}
		GetComponent<Image> ().color = color;
	}
}
